using Commons.Models.Definitions;
using Commons.Models.Stats;
using System.Collections.Generic;
using Commons.Models.TradeValues;
using System;
using System.Runtime.Serialization;

namespace Commons.Models.Settings
{
	[Serializable]
	public class StatSettings : ISettings
	{
		private Dictionary<IStatUnit, IStatDefinition> definitions = new Dictionary<IStatUnit, IStatDefinition>();

		public StatSettings(List<IStatDefinition> definitions)
		{
			foreach (IStatDefinition stat in definitions)
			{
				this.definitions.Add(stat.Unit, stat);
			}
		}

		public IStatDefinition GetDefinition(IStatUnit unit)
		{
			try
			{
				return definitions[unit];
			}
			catch(KeyNotFoundException ex)
			{
				throw new KeyNotFoundException("The stat '"+unit.Name+"' was not found", ex);
			}
		}

		public T GetDefinition<T>(IStatUnit unit) where T : IStatDefinition
		{
			try
			{
				return (T) definitions[unit];
			}
			catch(KeyNotFoundException ex)
			{
				throw new KeyNotFoundException("The stat '"+unit.Name+"' was not found", ex);
			}
		}

		public IEnumerable<IStatDefinition> GetDefinitions()
		{
			return definitions.Values;
		}
	}
}
